using System;

namespace Outcoding.GraphGenerator.Entities {
    public class Util {

        /// <summary>
        /// Converts a color from a string (html format) to an integer
        /// </summary>
        /// <returns></returns>
        public static long ColorTranslation(string HTMLColor) {
            long R;
            long G;
            long B;
            long ColorNum;

            R = System.Convert.ToInt64(HTMLColor.Substring(1, 2), 16);
            G = System.Convert.ToInt64(HTMLColor.Substring(3, 2), 16);
            B = System.Convert.ToInt64(HTMLColor.Substring(5, 2), 16);
            ColorNum = ((B * 256) + G) * 256 + R;

            return ColorNum;
        }

        public static T ParseEnum<T>(string source)
        {
            if(!typeof(T).IsEnum)
                throw new NotSupportedException("Type to convert must be an Enum.");
            if(String.IsNullOrEmpty(source))
                throw new ArgumentNullException("Source string cannot be null or empty.");

            return (T)Enum.Parse(typeof(T), source);
        }

        public static T ParseEnum<T>(Enum source)
        {
            if (!typeof(T).IsEnum)
                throw new NotSupportedException("Type to convert must be an Enum.");
            if (source == null)
                throw new ArgumentNullException("Source cannot be null.");

            return (T)Enum.Parse(typeof(T), source.ToString("g"));
        }
    }
}
